<template>
  <p-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <p-form :form="form">
        <p-row>

		  <p-col  :span="12">
			<p-form-item label="工序工单编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['mobillno', validatorRules.mobillno]" :trigger-change="true" dictCode="mes_mobillno" placeholder="请选择工序工单编号"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="生产工单" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['productionOrder', validatorRules.productionOrder]" :trigger-change="true" dictCode="mes_production_order" placeholder="请选择生产工单"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="入库库位" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['storageLocation', validatorRules.storageLocation]" :trigger-change="true" dictCode="mes_storage_location" placeholder="请选择入库库位"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="入库仓库" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['storageWarehouse', validatorRules.storageWarehouse]" :trigger-change="true" dictCode="mes_storage_warehouse" placeholder="请选择入库仓库"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="物料编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<f-popup
						contextPath="basic-business"
						v-decorator="['mcode', validatorRules.mcode]"
						:trigger-change="true"
						org-fields="id,mcode,mname,sprc,mexplain"
						dest-fields="mid,mcode,mname,sprc,mexplain"
						code="wms_cpinfo"
						@callback="popupCallback"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'mname', validatorRules.mname]" placeholder="请输入物料名称"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="物料规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'sprc', validatorRules.sprc]" placeholder="请输入物料规格型号"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="物料描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'mexplain', validatorRules.mexplain]" placeholder="请输入物料描述"></p-input>
			</p-form-item>
		  </p-col>


        </p-row>
      </p-form>

      <!-- 子表单区域 -->
      <p-tabs v-model="activeKey" @change="handleChangeTabs">
        <p-tab-pane tab="半成品入库送检产品明细" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="mesMfDetailTable.loading"
            :columns="mesMfDetailTable.columns"
            :dataSource="mesMfDetailTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
      </p-tabs>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'MesMfWarehousingSemiFinishedProductsModal',
    mixins: [JEditableTableMixin],
    components: {
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          mobillno: {rules: [
          ]},
          productionOrder: {rules: [
          ]},
          storageLocation: {rules: [
          ]},
          storageWarehouse: {rules: [
          ]},
          mcode: {rules: [
            {required: true, message: '请输入物料编码!'},
          ]},
          mname: {rules: [
            {required: true, message: '请输入物料名称!'},
          ]},
          sprc: {rules: [
          ]},
          mexplain: {rules: [
          ]},
        },
        refKeys: ['mesMfDetail', ],
        tableKeys:['mesMfDetail', ],
        activeKey: 'mesMfDetail',
        // 半成品入库送检产品明细
        mesMfDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '物料编码',
              key: 'mcode',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料描述',
              key: 'mname',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '线体编码',
              key: 'lineno',
              type: FormTypes.popup,
              contextPath: 'basic-business',
              popupCode: 'line_select',
              destFields: 'lineid,lineno,linename',
              orgFieldse: 'lineid,lineno,linename',
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '线体名称',
              key: 'linename',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '生产工单',
              key: 'productionOrder',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '工序工单编号',
              key: 'mobillno',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '栈板号/容器码',
              key: 'zbrq',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: 'SN/PN',
              key: 'snpn',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '数量',
              key: 'qty',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '状态',
              key: 'state',
              type: FormTypes.select,
              dictCode: 'mes_state',
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },

            {
              title: 'lineid',
              key: 'lineid',
              type: 'hidden'
            },


          ]
        },
        url: {
          add: '/mes-manufacture/auto/mesMfWarehousingSemiFinishedProducts/add',
          edit: '/mes-manufacture/auto/mesMfWarehousingSemiFinishedProducts/edit',
          mesMfDetail: {
            list: '/mes-manufacture/auto/mesMfWarehousingSemiFinishedProducts/queryMesMfDetailByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'mobillno','productionOrder','storageLocation','storageWarehouse','createBy','createTime','updateBy','updateTime','mcode','mname','sprc','mexplain')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.mesMfDetail.list, params, this.mesMfDetailTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          mesMfDetailList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'mobillno','productionOrder','storageLocation','storageWarehouse','createBy','createTime','updateBy','updateTime','mcode','mname','sprc','mexplain'))
     },

    }
  }
</script>

<style scoped>
</style>
